package com.qf.contract.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.*;
import com.qf.contract.mapper.FChangeRequestMapper;
import com.qf.contract.mapper.FContractDetailsMapper;
import com.qf.contract.mapper.FContractMapper;
import com.qf.contract.mapper.FSigningUnitMapper;
import com.qf.contract.qo.ContractInformationQueryQo;
import com.qf.contract.service.ContractInformationQueryService;
import com.qf.contract.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 合同信息查询
 */
@Service
@Slf4j
public class ContractInformationQueryServiceImpl implements ContractInformationQueryService {
    @Resource
    FContractMapper fContractMapper;
    @Resource
    FChangeRequestMapper fChangeRequestMapper;
    @Resource
    FContractDetailsMapper fContractDetailsMapper;
    @Resource
    FSigningUnitMapper fSigningUnitMapper;

    @Override
    public IPage<ContractInformationQueryVo> contractInformationQuery(ContractInformationQueryQo contractInformationQueryQo, int current, int size) {
        IPage<ContractInformationQueryVo> page;
        try {
            MPJLambdaWrapper<FContract> wrapper = new MPJLambdaWrapper<FContract>()
                    .selectAll(FContract.class)//查询user表全部字段
                    .selectAll(FContractDetails.class)
                    .selectAll(FTypeOfContract.class)
                    .selectAll(FSigningUnit.class)
                    .leftJoin(FContractDetails.class, FContractDetails::getContractId, FContract::getContractDetails)
                    .leftJoin(FTypeOfContract.class, FTypeOfContract::getContractId, FContract::getTypeOfContract)
                    .leftJoin(FSigningUnit.class, FSigningUnit::getUnitId, FContract::getSigningUnit);
            if (contractInformationQueryQo.getContractName() != null) {
                wrapper.like("contract_name", contractInformationQueryQo.getContractName());
            }

            if (contractInformationQueryQo.getTypeContractName() != null) {
                wrapper.like("type_contract_name", contractInformationQueryQo.getTypeContractName());
            }
            if (contractInformationQueryQo.getUnitName() != null) {
                wrapper.like("unit_name", contractInformationQueryQo.getUnitName());
            }
            if (contractInformationQueryQo.getContractAmount() != null) {
                wrapper.like("contract_amount", contractInformationQueryQo.getContractAmount());
            }
            if (contractInformationQueryQo.getContractStatus() != null) {
                wrapper.like("contract_status", contractInformationQueryQo.getContractStatus());
            }
            if (contractInformationQueryQo.getContractSigningDepartment() != null) {
                wrapper.like("contract_signing_department", contractInformationQueryQo.getContractSigningDepartment());
            }
            //TODO 分页连表查询
            page = fContractMapper.selectJoinPage(new Page<>(current, size), ContractInformationQueryVo.class, wrapper);
            //对查询的结果做处理
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return page;
    }

    //TODO 合同详细信息显示

    /**
     * 基本信息
     */
    @Override
    public List<ContractDetailsDisplayVo> basicInformationQuery(int id) {
        //查询合同表
        FContract fContract = new FContract();
        fContract.setContractId(id);
        try {
            MPJLambdaWrapper<FContract> wrapper = new MPJLambdaWrapper<FContract>()
                    .selectAll(FContract.class)//查询user表全部字段
                    .selectAll(FTypeOfContract.class)
                    .leftJoin(FTypeOfContract.class, FTypeOfContract::getContractId, FContract::getTypeOfContract);

            //查询数据
            //将数据从po转换成vo
            List<ContractDetailsDisplayVo> contractDetailsDisplayVos = fContractMapper.selectJoinList(ContractDetailsDisplayVo.class, wrapper);
            for (ContractDetailsDisplayVo contractDetailsDisplayVo : contractDetailsDisplayVos) {
                contractDetailsDisplayVo.setPeriodOfValidity((int) ((contractDetailsDisplayVo.getContractStopDate().getTime() - contractDetailsDisplayVo.getContractSigningDate().getTime()) * (1000 * 60 * 60 * 24 * 365)));
                log.info(String.valueOf(contractDetailsDisplayVo.getPeriodOfValidity()));
            }
            return contractDetailsDisplayVos;
            //对查询的结果做处理
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 签订单位明细
     *
     * @param id 合同表id
     */
    @Override
    public SigningUnitQueryVo signingUnitQuery(int id) {

        try {
            SigningUnitQueryVo signingUnitQueryVo = new SigningUnitQueryVo();
            //查询合同表
            FContract fContract = new FContract();
            fContract = fContractMapper.selectById(id);
            FSigningUnit fSigningUnit = fSigningUnitMapper.selectById(fContract.getSigningUnit());
            BeanUtils.copyProperties(fSigningUnit, signingUnitQueryVo);
            return signingUnitQueryVo;
            //对查询的结果做处理
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 合同明细查询
     */
    @Override
    public ContractDetailsQueryVo contractDetailsQuery(int id) {
        /**
         * 思路，通过合同表去获取明细表id
         * 通过明细表id去读取明细表
         */
        ContractDetailsQueryVo contractDetailsQueryVo = new ContractDetailsQueryVo();
        try {
            //查询合同明细表

            FContract fContract = fContractMapper.selectById(id);

            FContractDetails fContractDetails = fContractDetailsMapper.selectById(fContract.getContractDetails());

            BeanUtils.copyProperties(fContractDetails, contractDetailsQueryVo);
            return contractDetailsQueryVo;

            //查询数据
            //将数据从po转换成vo         //对查询的结果做处理
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 变更信息查询
     *
     * @param id
     * @return
     */
    @Override
    public List<ChangeInformationVo> changeInformation(int id) {
        /**
         * 实体类表FChangeRequest
         *  * 通过合同变更表查询有没有这个合同id
         */
        try {
            MPJLambdaWrapper<FChangeRequest> wrapper = new MPJLambdaWrapper<>();
            wrapper.eq("contract_id", id);
            List<ChangeInformationVo> changeInformationVos = fChangeRequestMapper.selectJoinList(ChangeInformationVo.class, wrapper);
            return changeInformationVos;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }
}
